package com.weacar.weixin.controller;

import com.weacar.weixin.model.Bonus;
import com.weacar.weixin.service.BonusService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.ArrayList;
import java.util.Optional;

/**
 * @author Fong
 */
@Controller
@RequestMapping("/admin/bonus")
public class BonusController {

    @Autowired
    private BonusService bonusService;


    @RequestMapping("/query")
    public String queryBonus() throws Exception {
        return "bonus/query";
    }


    @RequestMapping("/list")
    public String allBonus(
            @RequestParam("phone") List<String> phoneList,
            ModelMap modelMap) throws Exception {
        List<Bonus> bonusList = new ArrayList<Bonus>();
        for (String phone : phoneList) {
            if (StringUtils.isNotBlank(phone)) {
                Optional<Bonus> bonus = bonusService.getBonusByPhone(phone);
                if (bonus.isPresent()) {
                    bonusList.add(bonus.get());
                }
            }
        }
        modelMap.put("bonusList", bonusList);
        return "bonus/list";
    }


}
